from fastapi import HTTPException, status 
from jose import JWTError, jwt 
from pydantic import BaseModel, ValidationError 

SECRET_KEY = 'la3rwln7VA%A9v*NC^$FX6J5QtW^T!B4'
ALGORITHM = 'HS256'

class AuthTokenHelper: 

    @staticmethod 
    def token_encode(data): 
        
        return jwt.encode(data, SECRET_KEY, algorithm=ALGORITHM) 

    @staticmethod 
    def token_decode(token): 
        credentials_exception = HTTPException(
            status_code=401, 
            detail='Could not validata credentials', 
            headers={'WWW-Authenticate': 'Bearer'}, 
        )
        try: 
            payload = jwt.decode(token=token, key=SECRET_KEY, algorithms=[ALGORITHM])
        except (JWTError, ValidationError): 
            raise credentials_exception 
        
        return payload 
    

if __name__ == '__main__': 
    print(AuthTokenHelper.token_encode({'user': 'XZSD'}))
    print(AuthTokenHelper.token_decode(AuthTokenHelper.token_encode({'user': 'XZSD'})))
        